package com.azt.front.action.pingan;

import com.azt.api.pojo.Company;
import com.azt.api.pojo.PinganAccount;
import com.azt.api.service.PinganAccountService;
import com.azt.api.service.PinganAmountInService;
import com.azt.api.service.PinganAmountOutService;
import com.azt.exception.BaseException;
import com.azt.front.action.BaseAction;
import com.azt.front.permission.ClearRequire;
import com.azt.front.permission.RequireCompany;
import com.azt.front.servlet.Server;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.utils.DESUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.Socket;
//                            _ooOoo_
 //                           o8888888o
 //                           88" . "88
 //                           (| -_- |)
 //                            O\ = /O
 //                        ____/`---'\____
 //                      .   ' \\| |// `.
 //                       / \\||| : |||// \
 //                     / _||||| -:- |||||- \
 //                       | | \\\ - /// | |
 //                     | \_| ''\---/'' | |
 //                      \ .-\__ `-` ___/-. /
 //                   ___`. .' /--.--\ `. . __
 //                ."" '< `.___\_<|>_/___.' >'"".
 //               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 //                 \ \ `-. \_ __\ /__ _/ .-` / /
 //         ======`-.____`-.___\_____/___.-`____.-'======
 //                            `=---='
 //
 //         .............................................
 //                  佛祖保佑             永无BUG
 //          佛曰:
 //                  写字楼里写字间，写字间里程序员；
 //                  程序人员写程序，又拿程序换酒钱。
 //                  酒醒只在网上坐，酒醉还来网下眠；
 //                  酒醉酒醒日复日，网上网下年复年。
 //                  但愿老死电脑间，不愿鞠躬老板前；
 //                  奔驰宝马贵者趣，公交自行程序员。
 //                  别人笑我忒疯癫，我笑自己命太贱；
 //                  不见满街漂亮妹，哪个归得程序员？

/**
 *
 * Created by zouheyuan on 2017/1/18.
 * 平安银行线上支付业务层
 *
 */
@Controller
@RequireCompany(state = {Company.AUDIT_STATE_PASS})
@RequestMapping("/pingan")
public class PinganAction extends BaseAction{

    @Autowired
    private PinganAccountService pinganAccountService;

    @Autowired
    private PinganAmountOutService pinganAmountOutService;

    @Autowired
    private PinganAmountInService pinganAmountInService;


    public static Logger logger = LoggerFactory.getLogger("paInfo");


    /**
     * 我的平安银行账户
     * @return
     */
    @RequestMapping( value = "account")
    public String myAccount(Model model){
        PinganAccount account = pinganAccountService.getValidAccountByCompanyId(ContextUtil.getCompanyId());
        if (account != null) {
            model.addAttribute("account", account);
            return "pingan/myAccount";
        }

        model.addAttribute("companyCode" , DESUtils.encryptCompanyId(ContextUtil.getCompanyId()));
//
//        Thread t1 = new Thread(new OutputRuner(39999));
//        t1.start();

        return "pingan/unSign";
    }


    /**
     * 交易网发起入金方法
     * @return
     */
    @RequestMapping( value = "/amountIn" , method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson amountIn(){
        Integer companyId = ContextUtil.getCompanyId();
        PinganAccount account = pinganAccountService.getValidAccountByCompanyId(companyId);//根据企业获取平安银行签约
        if (account != null) {
            Double tranAmount = getParaToDouble("tranAmount", 0d);
            if(tranAmount.compareTo(0d) > 0 && ("2".equals(account.getAcctFlag()) || "3".equals(account.getAcctFlag()))){
                try {
                    pinganAmountInService.amountIn(account , tranAmount , ContextUtil.getUserId());//入金功能
                    return AjaxJson.success();
                } catch (BaseException e) {
//                    e.printStackTrace();
                    return AjaxJson.success(e.getErrorCode());
                }
            }else {
                return AjaxJson.error("金额不正确或签约账号为非入金账号");
            }
        } else {
            return AjaxJson.error("企业未签约");
        }
    }


    /**
     * 交易网发起出金方法
     * @return
     */
    @RequestMapping( value = "/amountOut" , method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson amountOut(){
        Integer companyId = ContextUtil.getCompanyId();
        PinganAccount account = pinganAccountService.getValidAccountByCompanyId(companyId);//根据企业获取平安银行签约
        if (account != null) {
            Double tranAmount = getParaToDouble("tranAmount", 0d);
            if(tranAmount.compareTo(0d) > 0
                    && account.getTranAmount().compareTo(tranAmount) >= 0
                    && ("1".equals(account.getAcctFlag()) || "3".equals(account.getAcctFlag()))){
                try {
                    pinganAmountOutService.amountOut(account , tranAmount , ContextUtil.getUserId());//出金功能
                    return AjaxJson.success();
                } catch (BaseException e) {
//                    e.printStackTrace();
                    return AjaxJson.success(e.getErrorCode());
                }
            }else {
                return AjaxJson.error("金额不正确或余额不足或签约账号为非出金账号");
            }
        } else {
            return AjaxJson.error("企业未签约");
        }
    }


    /**
     * 交易网发起查询可入金资金
     * @return
     */
    @RequestMapping( value = "/accountCanIn" , method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson accountCanIn(){
        Integer companyId = ContextUtil.getCompanyId();
        PinganAccount account = pinganAccountService.getValidAccountByCompanyId(companyId);//根据企业获取平安银行签约
        if (account != null) {
                try {
                    double canIn = pinganAccountService.accountCanIn(account);
                    return AjaxJson.success().setObj(canIn);
                } catch (Exception e) {
//                    e.printStackTrace();
                    return AjaxJson.success(e.getMessage());
                }
        } else {
            return AjaxJson.error("企业未签约");
        }
    }




    /**
     * 复合支付
     * this is the most important method
     * God bless me
     * @return
     */
    @RequestMapping(value = "/reviewPay", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson reviewPay() {
        return AjaxJson.success();
    }


    /**
     * 解冻功能
     *
     * @param payNumber    支付订单流水   唯一依据
     * @return
     */
    @ClearRequire
    @RequestMapping(value = "/thaw")
    @ResponseBody
    public AjaxJson thaw(String payNumber) {
        try {
            pinganAccountService.thawMethod(payNumber);
            return AjaxJson.success();
        } catch (BaseException e) {
            return AjaxJson.error(e.getErrorCode());
        } catch (Exception ex) {
            return AjaxJson.error("未知Exception错误"+ex.getMessage());
        }
    }


    /**
     * 提醒卖家使用线上支付
     * @param sellerCompanyId    卖家企业id
     * @param buyerCompanyId    买家企业id
     * @return
     */
    @RequestMapping(value = "/remind")
    @ResponseBody
    public AjaxJson remind(Integer sellerCompanyId , Integer buyerCompanyId) {
        try {
            if (sellerCompanyId == null || buyerCompanyId == null) {
                return AjaxJson.error("数据有误");
            }
            pinganAccountService.reminSeller(sellerCompanyId, buyerCompanyId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.error(e.getMessage());
        }

        return AjaxJson.success();
    }






    /**
     * 平安银行每天凌晨三点进行账户信息对账
     * 核对交易网和平安银行间账户的一致。
     */
    @ClearRequire
    @RequestMapping(value = "/quartzCheckAccount")
    public void checkAccountQuartz() {
        pinganAccountService.checkAccountQuartz();
    }

    /**
     * 平安银行每天凌晨四点进行交易信息对账
     * 核对交易网和平安银行间交易的一致。
     */
    @ClearRequire
    @RequestMapping(value = "/quartzCheckBus")
    public void checkBusQuartz() {
        pinganAccountService.checkBusQuartz();
    }


    @ClearRequire
    @RequestMapping(value = "/checkBusQuartzForNo")
    public void checkBusQuartzForNo(String no) {
        pinganAccountService.checkBusiness(no, "20170321", "20170321", 1);
    }

    @ClearRequire
    @RequestMapping(value = "/check1021")
    public void check1021() {
        pinganAccountService.getuperviseInfo();
    }

    @ClearRequire
    @RequestMapping(value = "/check1016")
    public void check1016() {
        pinganAccountService.checkOpenCloseAccount("20170321" , "20170321" , 1);
    }

    @ClearRequire
    @RequestMapping(value = "/check1020")
    public void check1020() {
        pinganAccountService.accountCanIn(pinganAccountService.getValidAccountByCompanyId(3699));
    }

    @ClearRequire
    @RequestMapping(value = "/check1325")
    public void check1325() {
        pinganAccountService.checkAmountInOut(null , "20170321" , "20170321" , 1);
    }

    @ClearRequire
    @RequestMapping(value = "/check1327")
    public void check1327() {
        pinganAccountService.getPaySerialNoState("790");
    }

    @ClearRequire
    @RequestMapping(value = "/check1349")
    public void check1349() {
        pinganAccountService.geteconciliationPwd("20170321" , 20);
    }

    @ClearRequire
    @RequestMapping(value = "/checkfile03")
    public void checkfile03() {
        pinganAccountService.checkFile03("CRJ898120170320.txt" , "PKz24wATYXa6hmowMLrAVWAhnK2ZfYiw");
    }




}